<#ftl/>
package ${serviceImplPackage};

import ${entityPackage}.${entity};
<#if need_saveDto>
import ${saveDtoPackage}.${saveDtoClassName};
import ${voPackage}.${voClassName};
</#if>
import ${mapperPackage}.${mapperClassName};
import ${servicePackage}.${serviceClassName};
import com.qhkj.engineering.common.exception.ServiceException;
import com.qhkj.engineering.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.Date;
import java.util.Set;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * ${table.comment!} 服务类
 * </p>
 * @author ${author}
 * @since ${date}
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ${serviceImplClassName} implements ${serviceClassName} {

    @Resource
    private ${mapperClassName} ${mapperClassName?uncap_first};
<#if need_saveDto>

    public int add(${saveDtoClassName} dto) {
        ${entity} entity=new ${entity}();
        BeanUtils.copyProperties(dto,entity);
    <#assign time=false/>
    <#list table.fields as field>
        <#if field.propertyName=='createTime'||field.propertyName=='createBy'>
            <#assign time=true/></#if>
            </#list>
        <#list table.fields as field><#if field.keyFlag><#--主键java类型-->
        entity.set${field.propertyName?cap_first}(null);
        </#if></#list>
    <#if time>
        entity.setCreateTime(LocalDateTime.now());
        entity.setCreateBy(SecurityUtils.getUsername());
    </#if>
        return ${mapperClassName?uncap_first}.insert(entity);
    }

    public int update(${saveDtoClassName} dto) {
    <#list table.fields as field><#if field.keyFlag><#--主键java类型-->
        if(dto.get${field.propertyName?cap_first}()==null){
            throw new ServiceException("修改${field.propertyName}不能为空");
        }</#if></#list>
        ${entity} entity=new ${entity}();
        BeanUtils.copyProperties(dto,entity);<#assign time=false/>
<#list table.fields as field><#if field.propertyName=='updateTime'||field.propertyName=='updateBy'>   <#assign time=true/></#if></#list><#if time>
    entity.setUpdateTime(LocalDateTime.now());
    entity.setUpdateBy(SecurityUtils.getUsername());
    </#if>
        return ${mapperClassName?uncap_first}.updateById(entity);
    }
    
    public void delete(<#list table.fields as field><#if field.keyFlag><#--主键java类型-->List<${field.columnType.type}> idList</#if></#list>){
        if(CollectionUtils.isEmpty(idList)){
            return;
        }
        ${mapperClassName?uncap_first}.deleteBatchIds(idList);
    }
    
    public ${voClassName} getById(<#list table.fields as field><#if field.keyFlag><#--主键java类型-->${field.columnType.type} id</#if></#list>) {
        ${entity} entity = ${mapperClassName?uncap_first}.selectById(id);
        if (ObjectUtils.isEmpty(entity)){
            throw new RuntimeException("该${table.comment!}不存在");
        }
        return toVo(entity);
    }
    
    public List<${voClassName}> queryList(${entity} query) {

        return ${mapperClassName?uncap_first}.query(query);
    }

    private ${voClassName} toVo(${entity} entity){
        ${voClassName} vo=new ${voClassName}();
        BeanUtils.copyProperties(entity,vo);
        return vo;
    }
<#else>
    public int add(${entity} entity) {
    <#list table.fields as field><#if field.keyFlag><#--主键java类型-->
        entity.set${field.propertyName?cap_first}(null);</#if></#list>
        entity.setCreateTime(LocalDateTime.now());
        entity.setCreateBy(SecurityUtils.getUsername());
        return ${mapperClassName?uncap_first}.insert(entity);
    }

    public int update(${entity} entity) {
    <#list table.fields as field><#if field.keyFlag><#--主键java类型-->
        if(entity.get${field.propertyName?cap_first}()==null){
            throw new ServiceException("修改${field.propertyName}不能为空");
        }</#if></#list>
        entity.setUpdateTime(LocalDateTime.now());
        entity.setUpdateBy(SecurityUtils.getUsername());
        return ${mapperClassName?uncap_first}.updateById(entity);
    }

    public void delete(<#list table.fields as field><#if field.keyFlag><#--主键java类型-->List<${field.columnType.type}> idList</#if></#list>){
        if(CollectionUtils.isEmpty(idList)){
            return;
        }
        ${mapperClassName?uncap_first}.deleteBatchIds(idList);
    }

    public ${entity} getById(<#list table.fields as field><#if field.keyFlag><#--主键java类型-->${field.columnType.type} id</#if></#list>) {
        ${entity} entity = ${mapperClassName?uncap_first}.selectById(id);
        if (ObjectUtils.isEmpty(entity)){
            throw new RuntimeException("该${table.comment!}不存在");
        }
        return entity;
    }

    public List<${entity}> queryList(${entity} query) {
        return ${mapperClassName?uncap_first}.query(query);
    }

</#if>
    

    
}

